package com.loop.unclelend.ui.activity

import android.graphics.Color
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.loop.unclelend.R
import com.loop.unclelend.databinding.UiPermissionBinding
import com.loop.unclelend.databinding.UncleItemPermissionIndicatorBinding
import com.loop.unclelend.pro.ProActivity
import com.loop.unclelend.pro.ProFragment
import com.loop.unclelend.ui.fragment.Fm_PermissionPage
import com.loop.unclelend.ui.presenter.Pre_Permission
import com.loop.unclelend.ui.view.IView_Permission
import com.loop.unclelend.utils.BarUtil
import com.loop.unclelend.utils.event.UncleEventHelper
import com.loop.unclelend.utils.event.UncleEventType
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/4 14:23
 * @Annotation：权限
 */
class Act_Permission: ProActivity(),IView_Permission {
    lateinit var binding: UiPermissionBinding

    var presenter: Pre_Permission? = null
    var pageList: ArrayList<ProFragment>? = null

    override fun getUncleRootView(): View? {
        binding = UiPermissionBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initUncleView() {
        BarUtil.setStatusBarLightMode(this, false)
        rootViewBinding.layoutRoot.setBackgroundColor(Color.parseColor("#FF353535"))
        pageList = arrayListOf()
    }

    override fun initUncleListener() {
        binding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                presenter!!.onUncleCheckPageClick(position)
            }
        })

        binding.checkBox.setOnCheckedChangeListener { _, isChecked ->
            when (isChecked) {
                true -> {
                    binding.btnAgree.setBackgroundResource(R.drawable.circle_fill_black)
                    binding.btnAgree.isEnabled = true
                }
                false -> {
                    binding.btnAgree.setBackgroundResource(R.drawable.circle_fill_ffe6e4e9)
                    binding.btnAgree.isEnabled = false
                }
            }
        }
        //拒绝
        binding.btnDeny.setOnClickListener {
            presenter!!.onUncleDenyClick()
        }

        //同意权限
        binding.btnAgree.setOnClickListener {
            presenter!!.onUncleAgreeClick()
        }

        //已授权
        binding.layoutAuthorized.setOnClickListener{
        }

        //被禁止
        binding.layoutBanned.setOnClickListener {
            presenter!!.onUncleBannedClick()
        }
    }

    override fun initUncleData() {
        presenter = Pre_Permission(this)
        presenter!!.loadUnclePermissionListAction()
    }

    override fun onResume() {
        super.onResume()
        presenter!!.onUnclePermissionResume()
    }

    /**
     * 设置权限列表
     */
    override fun showPermissionList(pList: ArrayList<JSONObject>) {
        pList.forEach {
            pageList!!.add(Fm_PermissionPage(it))
        }
        val pageAdapter = MyFragmentPagerAdapter(this,pageList!!)
        binding.viewPager.adapter = pageAdapter
    }

    /**
     * 设置显示权限
     */
    override fun showPermissionPage(index: Int) {
        binding.viewPager.setCurrentItem(index)
        addIndicatorViews(index)

        val item = presenter!!.getUnclePermissionList()[index]
        binding.imgPermission.setImageResource(item.optInt("icon"))
        binding.txtPermission.text = item.optString("title")
        binding.txtPermission.setTextColor(item.optInt("titleColor"))
    }

    /**
     * 未申请
     */
    override fun showPermissionNotObtained() {
        binding.layoutDefault.visibility = View.VISIBLE
        binding.layoutAuthorized.visibility = View.GONE
        binding.layoutBanned.visibility = View.GONE

        binding.layoutReadTips.visibility = View.VISIBLE

        binding.checkBox.isChecked = true
        binding.btnAgree.setBackgroundResource(R.drawable.circle_fill_black)
        binding.btnAgree.isEnabled = true
    }

    /**
     * 已允许
     */
    override fun showPermissionAuthorized() {
        binding.layoutDefault.visibility = View.GONE
        binding.layoutAuthorized.visibility = View.VISIBLE
        binding.layoutBanned.visibility = View.GONE

        binding.layoutReadTips.visibility = View.GONE
    }

    /**
     * 已拒绝
     */
    override fun showPermissionBanned() {
        binding.layoutDefault.visibility = View.GONE
        binding.layoutAuthorized.visibility = View.GONE
        binding.layoutBanned.visibility = View.VISIBLE

        binding.layoutReadTips.visibility = View.GONE
    }

    /**
     * 进入登录页面
     */
    override fun showLoginPage() {
        onIntent(Act_Login::class.java)
        finish()
    }

    /**
     * 添加指示器
     */
    private fun addIndicatorViews(index: Int){
        val item = presenter!!.getUnclePermissionList()[index]
        binding.layoutIndicator.removeAllViews()
        for (i in 0..<pageList!!.size){
            val indicatorBinding = UncleItemPermissionIndicatorBinding.inflate(layoutInflater)
            when (i){
                index -> {
                    indicatorBinding.imgView.setImageResource(R.drawable.uncle_permission_indicator_select)
                }
                else -> {
                    indicatorBinding.imgView.setImageResource(R.drawable.uncle_permission_indicator_normal)
                }
            }
            binding.layoutIndicator.addView(indicatorBinding.root)
        }
    }


    class MyFragmentPagerAdapter(fragmentActivity: FragmentActivity,val pageList: ArrayList<ProFragment>) : FragmentStateAdapter(fragmentActivity) {
        override fun createFragment(position: Int): Fragment {
            return pageList[position]
        }

        override fun getItemCount(): Int {
            return pageList.size
        }
    }

    override fun onStart() {
        super.onStart()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.PAGE.UNCLE_PAGE_PERMISSION_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.PAGE.UNCLE_PAGE_PERMISSION_END)
    }
}